package com.enuo;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.enuo.utils.AjaxMsg;
import com.enuo.utils.WebUtils;

public class ExceptionResolver implements HandlerExceptionResolver
{
    private Logger logger = Logger.getLogger(ExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
    {
        logger.error("出现未处理的异常", ex);
        WebUtils.printRequestData(request);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        
        try
        {
            response.getWriter().write(AjaxMsg.failed("系统异常"));
        }
        catch(IOException e)
        {
            logger.error("与客户端通讯异常：" + e.getMessage(), e);
            e.printStackTrace();
        }

        return new ModelAndView();
    }
}
